package oas.web;

import oas.db.AttendanceRepository;
import oas.db.UserRepository;

import oas.domain.Attendance;
import oas.domain.LeaveType;
import oas.domain.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.Date;

/**
 * 签到的控制器
 * 
 * @author Dzq
 * @version v1.1.0
 */
@Controller
@RequestMapping("/attendances")
public class AttendanceController {
	
	@Autowired
	private AttendanceRepository attendanceRepository;
	
	private static Logger logger = Logger.getLogger("oas");
	
	/**
	 * 签到页面
	 * 
	 * @return 签到页面视图
	 */
	@RequestMapping(method = RequestMethod.GET)	
	public String showAttendance(HttpSession session,Model model) {
		User user = (User) session.getAttribute("user");
		Date today = new Date();
		boolean NotShowButton = attendanceRepository.findTodayAttendance(user, today);
		logger.debug(NotShowButton);
		model.addAttribute("NotShowButton", NotShowButton);
		return "attendances";
	}
	
	/**S
	 * 进行签到，签到成功返回签到成功页面
	 * @param session
	 * @return 签到成功页面视图
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String doSign(HttpSession session) {
		User user = (User) session.getAttribute("user");
		long id = 1;
		Date date = new Date();
		Date attendanceTime = new Date();
		String status = "1";
		Attendance attendance = new Attendance(id,user,date,attendanceTime,status);
		attendance = attendanceRepository.save(attendance);
		return "signSuccess";
	}
	
	/**
	 * 分页显示个人历史考勤记录
	 * @param pageNo 起始页面
	 * @param pageSize 页面大小
	 * @param model 前端使用的model
	 * @param session
	 * @return 历史考勤记录页面视图
	 */
	@RequestMapping(value = "/history", method = RequestMethod.GET)
	public String showAttendancesHistory(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		PaginationSupport<Attendance> ps = attendanceRepository.findPage(pageNo, pageSize, user);
		logger.debug(ps.getItems().toString());
		model.addAttribute("paginationSupport", ps);
		
		return "attendancesHistory";
	}
	
	/**
	 * 查看个人某一考勤记录详情
	 * @param id
	 * @param session
	 * @param model
	 * @return 考勤详情页面视图
	 */
	@RequestMapping(value = "/history/{id}", method = GET)
   	public String showOneAttendance(@PathVariable("id") Long id ,HttpSession session,Model model) {
   		model.addAttribute(attendanceRepository.findById(id));
   		return "attendanceMessage";
   	}
	
	
}
